(function($){
  /**
   * Make the search box on the top menu navigation go to the friendly url search page.
   */
  Drupal.behaviors.map = {
    attach : function() {
      var canvas     =  document.getElementById("map_canvas"),
          latitude   =  jQuery(canvas).data('latitude'),
          longitude  =  jQuery(canvas).data('longitude'),
          latlng     =  new google.maps.LatLng(latitude, longitude),
          markers    =  [],
          iWindows   =  [],
          myOptions  =  {
            zoom       :  6,
            center     :  latlng,
            mapTypeId  :  google.maps.MapTypeId.ROADMAP
          },

          map = new google.maps.Map(canvas, myOptions);

          jQuery('.mod-map').find('input[name="marker"]').each(function(){
            var pos  =  new google.maps.LatLng( jQuery(this).data('lat'), jQuery(this).data('long') ),
                marker    =  new google.maps.Marker({
                  position  :  pos,
                  map       :  map,
                  title     :  jQuery(this).val()
                }),
                infowindow = new google.maps.InfoWindow({
                  content   :  jQuery(this).data('desc')
                });

                google.maps.event.addListener(marker, 'mouseover', function() {
                  infowindow.open(map,marker);
                });
                
                google.maps.event.addListener(marker, 'mouseout', function() {
                  infowindow.close();
                });
                
                markers.push(marker);  //let's store markers for outside usage
                iWindows.push(infowindow);
          });
          
      jQuery('.map-marker').click(function(){
        var pos = jQuery(this).data('index');
        window.location.hash = 'map';
        jQuery.each( markers, function( index, value ) {
          iWindows[index].close();
        });
        google.maps.event.trigger( markers[pos], 'mouseover' );
      });

      /*
       * @reference: http://code.google.com/apis/maps/documentation/javascript/overlays.html#RemovingOverlays
       */
      function addMarker(location) {
        marker = new google.maps.Marker({
          position : location,
          map      : map
        });
        markersArray.push(marker);
      }

      // Removes the overlays from the map, but keeps them in the array
      function clearOverlays() {
        if (markersArray) {
          for (i in markersArray) {
            markersArray[i].setMap(null);
          }
        }
      }

      // Shows any overlays currently in the array
      function showOverlays() {
        if (markersArray) {
          for (i in markersArray) {
            markersArray[i].setMap(map);
          }
        }
      }

      // Deletes all markers in the array by removing references to them
      function deleteOverlays() {
        if (markersArray) {
          for (i in markersArray) {
            markersArray[i].setMap(null);
          }
          markersArray.length = 0;
        }
      }

      //then to add markers read the data-markers (should return array) and loop trough each one
    }
  };
}(jQuery));